package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 柳文迪
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult selectPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //分页用第三方插件pageHelper
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        //根据条件进行查询
        List<Category> categoryList = categoryMapper.selectPage(categoryPageQueryDTO);

        Page pa = (Page) categoryList;


        PageResult pageResult = new PageResult(pa.getTotal(),pa.getResult());

        return pageResult;
    }

    /**
     * 根据Id删除分页
     * @param id
     */
    @Override
    public void deleteByIdCategory(Integer id) {

        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer count = dishMapper.countByCategoryId(id.longValue());
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        count = setmealMapper.countByCategoryId(id.longValue());
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除分类数据
        categoryMapper.deleteByIdCategory(id);
    }

    /**
     * 新增分类
     * @param category
     */
    @Override
    public void addCategory(CategoryDTO category) {
        //把剩余数据自己维护补充
        Category cat = new Category();
        //把前端获取到的数据传递给实体类
        BeanUtils.copyProperties(category,cat);
        //添加默认状态
        cat.setStatus(StatusConstant.DISABLE);
        //添加创建时间
        cat.setCreateTime(LocalDateTime.now());
        //添加结束时间
        cat.setUpdateTime(LocalDateTime.now());
        //添加创建人
        cat.setCreateUser(BaseContext.getCurrentId());
        //添加修改人
        cat.setUpdateUser(BaseContext.getCurrentId());

        //调用mapper层添加数据
        categoryMapper.addCategory(cat);
    }

    /**
     * 分类启动禁用功能
     * @param status
     * @param id
     */
    @Override
    public void StartupDisable(Integer status, Integer id) {
        //把前端接收的参数传递给一个实体类里
        Category categoryBuilder = Category.builder()
                .id(id.longValue())
                .status(status)
                .createUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId()).build();
        //调用mapper层更改数据库状态
            categoryMapper.StartupDisable(categoryBuilder);
    }

    /**
     * 修改分类
     * @param categoryId
     */
    @Override
    public void updateCategory(CategoryDTO categoryId) {
        Category category = new Category();
        //把获取到的值赋值给实体类
        BeanUtils.copyProperties(categoryId,category);
        //把剩余的属性添加上
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        //调用我们之前写过修改状态的修改类
        categoryMapper.StartupDisable(category);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }


}
